<?php

namespace App\Http\Controllers\Admin;

use App\Models\Currency;
use App\Models\Entrust;
use App\Models\Order;
use App\Models\Property;
use App\Models\User;
use App\Models\Message;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Cloud;
use Exception;
use App\Library\Orders\Orders;

use DB;

/**
 * 委托管理
 */
class EntrustController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'entrust');
    }

    /**
     * 获取本位币ID
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/13
     * @access public|private|protected
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function get_standard()
    {
        $map['is_standard'] = 1;
        $map['pay_switch'] = 1;
        $map['buy_switch'] = 1;
        $currency_info = Currency::where($map)->first(['id']);
        if (empty($currency_info)) {
            return response()->json(['status' => 0, 'msg' => '本位币错误']);
        }
        return $currency_info->id;
    }
    public function index(Request $request)
    {
        $like_account = '';
        $cid = '';
        $type = '';
        $status = '';
        if ($request->post('like_account')) {
            $like_account = $request->post('like_account');
        }
        if ($request->post('cid')) {
            $cid = $request->post('cid');
        }
        if ($request->post('type')) {
            $type = $request->post('type');
        }
        if ($request->post('status')) {
            $status = $request->post('status');
        }
        if (!empty($cid)) {//判断是否刚进入列表，刚进入没有cid
            if ($cid && $type && $status) {
                $where['status'] = $status;
                $where['type'] = $type;
                $where['cid'] = $cid;
            } elseif ($cid && empty($type) && $status) {
                $where['status'] = $status;
                $where['cid'] = $cid;
            } elseif ($cid && empty($type) && empty($status)) {
                $where['cid'] = $cid;
            } elseif ($cid && $type && empty($status)) {
                $where['type'] = $type;
                $where['cid'] = $cid;
            }
            $result       = Entrust::whereHas('user', function ($q) use ($like_account) {
                $q->where('account', 'like', '%' . $like_account . '%');
            })->where($where)->orderBy('id', 'desc')->paginate(10);
        } else {
            $result       = Entrust::whereHas('user', function ($q) use ($like_account) {
                $q->where('account', 'like', '%' . $like_account . '%');
            })->orderBy('id', 'desc')->paginate(10);
        }

        //dd($result);
        $data= [];
        foreach ($result as $k=>$value) {
            $data[$k]['number_no'] = $value->number_no;
            $data[$k]['id'] = $value->id;
            $data[$k]['account'] = $value->user->account;
            $data[$k]['name_en'] = $value->currency->name_en;
            $data[$k]['price'] = $value->price;
            $data[$k]['num'] = $value->num;
            $data[$k]['deal'] = $value->deal;
            $data[$k]['deal_time'] = $value->deal_time?date('Y-m-d H:i', (int)$value->deal_time):'-';
            $data[$k]['proc'] = $value->proc;
            $data[$k]['created_at'] = $value->created_at;
            $data[$k]['type'] = $value->type;
            $data[$k]['status'] = $value->status;
        }
        $price = Currency::get();
//        dd($data);
//        die;
        $url = ['status'=>$status,'type'=>$type,'cid'=>$cid,'like_account'=>$like_account];
        return view('admin/entrust/index', compact('data', 'price', 'result', 'url'));
    }

    /**
     * 查看委托单交易记录
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function entrust_details(Request $request)
    {
        $id = $request->get('id') ?? 0;
        $map1['eid'] = $id;
        $list       = Order::where($map1)->orderBy('id', 'desc')->paginate(10);
        foreach ($list as $k=>$v) {
            $data[$k]['u_name'] = $v->users->account;
            $data[$k]['name_en'] = $v->currency->name_en;
            $data[$k]['number_no'] = $v->number_no;
            $data[$k]['num'] = $v->num;
            $data[$k]['type'] = $v->type;
            $data[$k]['price'] = $v->price;
            $data[$k]['proc'] = $v->proc;
            $data[$k]['updated_at'] = $v->updated_at;
            $data[$k]['created_at'] = $v->created_at;
        }
//        dump($data);die;
        return view('admin/entrust/entrust_details', compact('data'));
    }

    /**
     * 取消委托
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/25
     * @access public|private|protected
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function entrust_undo(Request $request)
    {
        $info = $request->all();
        if (empty($info['id'])) {
            return ['msg'=>'网络延迟......','status'=>2];
        }
        $orders_model = new Orders();
        $re = $orders_model->cancel($info['id']);
        switch ($re['status']) {
            case 0:
                return ['msg'=>'数据有误','status'=>2];
            case 1:
                $msg_model = new Message();
                //生成通知消息
                $msg = [
                    'msg_id' => 26,
                    'uid' => $re['uid'],
                    'message' => [
                        $re['number_no']
                    ],
                    "arr"=> ['number' => $info['id']]
                ];
                $msg_model->syspublishsmsAll($msg);
                return ['msg'=>'操作成功','status'=>1];

            case 2:
                return ['msg'=>'取消失败','status'=>2];
        }
    }

    /**
     * 查看交易记录
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function entrust_order(Request $request)
    {
        $info = $request->all();
//        dump($info);
//        die;
        $like_account = '';
        if ($request->post('like_account')) {
            $like_account = $request->post('like_account');
        }
        $cid = '';
        if ($request->post('cid')) {
            $cid = $request->post('cid');
        }
        if (!empty($info['start_time'])){
            $start_time = $info['start_time'];
        }else{
            $start_time = '';
        }
        if (!empty($info['end_time'])){
            $end_time = $info['end_time'];
            $e_time = $info['end_time'];
        }else{
            $end_time =  date("Y-m-d H:i:s");
            $e_time = '';
        }
        if (empty($cid)) {
            $result       = Order::whereHas('users', function ($q) use ($like_account) {
                $q->where('account', 'like', '%' . $like_account . '%');
            })->whereBetween('created_at', [$start_time, $end_time])->orderBy('id', 'desc')->paginate(10);
        } else {
            $result       = Order::whereHas('users', function ($q) use ($like_account) {
                $q->where('account', 'like', '%' . $like_account . '%');
            })->where('cid', '=', $cid)->whereBetween('created_at', [$start_time, $end_time])->orderBy('id', 'desc')->paginate(10);
        }



        if ($result->isEmpty()){
            $data = null;
        }else{
            foreach ($result as $k=>$v) {
                if ($v->type==1){
                    $data[$k]['account_sid'] = $v->users->account;
                    $data[$k]['account'] = $v->users_sid->account;
                }else{
                    $data[$k]['account'] = $v->users->account;
                    $data[$k]['account_sid'] = $v->users_sid->account;
                }
                $data[$k]['name_en'] = $v->currency->name_en;
                $data[$k]['number_no'] = $v->number_no;
                $data[$k]['num'] = $v->num;
                $data[$k]['price'] = $v->price;
                $data[$k]['proc'] = $v->proc;
                $data[$k]['type'] = $v->type;
                $data[$k]['updated_at'] = $v->updated_at;
                $data[$k]['created_at'] = $v->created_at;
            }
        }

//        dd($data);
        $url = ['cid'=>$cid,'like_account'=>$like_account,'start_time'=>$start_time,'end_time'=>$e_time];
        $price = Currency::get();
        return view('admin/entrust/entrust_order', compact('data', 'price', 'result', 'url','cid','like_account','e_time','start_time'));
    }
}
